function Table(){
	this.whosAttackerId = 1;
	this.whosDefenderId = 2;
	this.whosTurn = 1;
}


Table.prototype.reloadSortedPlayerCards = function(playerId){
	var sortedPlayerCards = game.players[playerId].cards().sortCardByRanking();
	
	for(pc1=0;pc1<sortedPlayerCards.length;pc1++){
		if(Number(sortedPlayerCards[pc1].card_id)==0){
			break;
		}
		if(playerId==0){
			$("#p"+(1+playerId)+sortedPlayerCards[pc1].id).appendTo("#playerOneCards");
		}else{
			$("#p"+(1+playerId)+sortedPlayerCards[pc1].id).appendTo("#playerTwoCards");
		}
		$("#p"+(1+playerId)+sortedPlayerCards[pc1].id).css('margin-top','-100px');
	}
	$("#p"+(playerId+1)+sortedPlayerCards[0].id).css('margin-top','0px');
	
}


Table.prototype.getCurrentAttacker = function(){
	return "Player"+this.whosAttackerId;
}

Table.prototype.getCurrentDefender = function(){
	return "Player"+this.whosDefenderId;
}

Table.prototype.getCurrentTurn = function(){
	return "Player"+this.whosTurn;
}

Table.prototype.remainingCardsOnDeck = function(){
	return game.talonCards.getRemainingTalonCards();
}

Table.prototype.getTheMasterSuit = function(){
	return game.talonCards.getMasterSuit();
}

Table.prototype.getSuitValueOfCurrentMasterSuit = function(){
	var master  = this.getTheMasterSuit().card_id;
	console.log("getSuitValueOfCurrentMasterSuit = "+master);
	return game.cards.getSuit(master);
}

Table.prototype.whoDurak = function(){
	var p1CardsCount = game.players[0].cards().length;
	var p2CardsCount = game.players[1].cards().length;
	var talonCardCount = game.talonCards.getRemainingTalonCards().length;
	if(talonCardCount==0){
		if(p1CardsCount>p2CardsCount){
			if(p2CardsCount==0){
				// player 1 wins
				whosDurak.text("Players 1");
				alert("Player 1 DURAK!");
			}
		}else{
			if(p1CardsCount==0){
				// player 2 wins
				whosDurak.text("Players 2");
				alert("Player 2 DURAK!");
			}
		}
	}
}

Table.prototype.updateGameInformation = function(){
	attackerInfo.text(this.getCurrentAttacker());
	defenderInfo.text(this.getCurrentDefender());
	turnInfo.text(this.getCurrentTurn());
	var cards = this.remainingCardsOnDeck().length+' Cards'
	remainingCardsInfo.text(cards);
	var suitVal = this.getSuitValueOfCurrentMasterSuit();
	masterSuitInfo.text(suitVal);
	this.whoDurak();
}

//Table.prototype.informAttackWasEnded = function(){
//	
//}
// not working properly need some attention lately
Table.prototype.updatePlayerPrivileges = function(){
	//anonymous objects. created during runtime
	if(this.whosAttackerId==1){// for the mean time focused on player one scenario
		//player one config priv.
		attackToBecomeAttackFromDefender1.hide();
		endAttackp1.show();
		acceptCardsp1.hide();
		//player two config priv.
		attackToBecomeAttackFromDefender2.hide();
		endAttackp2.hide();
		acceptCardsp2.show();
	}else{// the attacker id player
		//player one config priv.
		attackToBecomeAttackFromDefender1.hide();
		endAttackp1.hide();
		acceptCardsp1.show();
		//player two config priv.
		attackToBecomeAttackFromDefender2.hide();
		endAttackp2.show();
		acceptCardsp2.hide();
	}
}

Table.prototype.addTracerInfo = function(info){//Info object
	tracer.append(info.getTableRow());
}
on = true;
Table.prototype.appendToRevealedTable = function(turnCardObject,playerId){
	var playerRow = $('#p'+playerId+turnCardObject.talon_id);
	playerRow.css('float','left');
	playerRow.attr('id','tc'+playerId+turnCardObject.talon_id);
	var count = game.cardsRevealed.length
	if(count>=3){
		playerRow.css('margin-top','-100px');
	}else{
		playerRow.css('margin-top','0px');
	}
	playerRow.appendTo(revealedTable);
	if(playerId==1){
		playerOneCards.find('div:nth-child(1)').css('margin-top','0px');
	}else{
		playerTwoCards.find('div:nth-child(1)').css('margin-top','0px');
	}
}

Table.prototype.reloadMasterSuit = function(){
	var suit = game.talonCards.getMasterSuit();
	masterSuit.attr('src',resImagesPathCards+suit.imageFilName);	
}

Table.prototype.reloadRevealedTableNoAjax = function(){
	
	game.cardsRevealed.updateRevealedCardsImageSrc();
	
	var cards = game.cardsRevealed;
//	revealedTable.html('');// clear data
//	var rowHead = $(newRow).attr('id','head');
//	rowHead.append($(newHead).text("Image"));
//	revealedTable.append(rowHead);
	if(cards.length==0){
		return;
	}
	$(cards).each(function(key,card){
		var div = $(newDiv).attr('id',"tc"+card.id); //talon id
		var img = $(newImage).attr('src',resImagesPathCards+card.imageFilName);
		img.attr('height',150);
		img.attr('width',100);
		div.append(img);
		div.css('float','left');
		var count = game.cardsRevealed.length
		revealedTable.css('margin-top','100px');
		div.css('margin-top','-100px');
		revealedTable.append(div);
	});
}

//single card inserttion to player table
Table.prototype.insertNewCardToPlayer = function(playerId, card){
	if(playerId==1)
	{
//		var btn = $(newBtn);
//		btn.attr('class','btn btn-danger');
//		btn.attr('id','btn1');
//		btn.attr('value','Use me');
//		btn.attr('onClick','game.players[0].attack('+card.id+')');
		
		var div = $(newDiv).attr('id',"p1"+card.id); //talon id
		var playerCards = game.players[1].cards().length;
		if(playerCards>0){
			div.css('margin-top','-100px');
		}
		var img = $(newImage).attr('src',resImagesPathCards+card.imageFilName);
		img.attr('height',150);
		img.attr('width',100);
		img.attr('onClick','game.players[0].attack('+card.id+')');
		div.append($(newData).append(img));
//		row.append($(newData).append(btn));
		playerOneCards.append(div);

	}else{
		
//		var btn = $(newBtn);
//		btn.attr('class','btn btn-danger');
//		btn.attr('id','btn1');
//		btn.attr('value','Use me');
//		btn.attr('onClick','game.players[1].attack('+card.id+')');
		
		var div = $(newDiv).attr('id',"p2"+card.id); //talon id
		var playerCards = game.players[1].cards().length;
		if(playerCards>0){
			div.css('margin-top','-100px');
		}
//		row.append($(newData).text(card.id));
		var img = $(newImage).attr('src',resImagesPathCards+card.imageFilName);
		img.attr('height',150);
		img.attr('width',100);
		img.attr('onClick','game.players[1].attack('+card.id+')');
		div.append($(newData).append(img));
		
//		row.append($(newData).append(btn));
		playerTwoCards.append(div);
	}
}

Table.prototype.removeTurnData = function(){
	
}

Table.prototype.appenCardToPlayer = function(cardid,playerId){
	
	if(playerId==1)
	{
//		var btn = $(newBtn);
//		btn.attr('class','btn btn-danger');
//		btn.attr('id','btn1');
//		btn.attr('value','Use me');
//		btn.attr('onClick','game.players[0].attack('+cardid+')');
//		if(this.whosTurn==2){
//			btn.css('display','none');
//		}
		var count = game.talonCards.getCardsbyPlayerId(playerId).length;
		
		var t = $('#talon'+cardid).attr('id','p1'+cardid);//.append(row);
		if(count<=1){
			t.css('margin-top','0px')
		}else{
			t.css('margin-top','-100px')
		}
		t.attr('onClick','game.players[0].attack('+cardid+')');
		t.appendTo('#playerOneCards');
		
		//$('#talon'+cardid).attr('id','#p1'+cardid).appendTo('#playerOneCards').fadeIn(1000);
	}else{
//		var btn = $(newBtn);
//		btn.attr('class','btn btn-danger');
//		btn.attr('id','btn2');
//		btn.attr('value','Use me');
//		btn.attr('onClick','game.players[1].attack('+cardid+')');
//		if(this.whosTurn==1){
//			btn.css('display','none');
//		}
		var count = game.talonCards.getCardsbyPlayerId(playerId).length;
		var t = $('#talon'+cardid).attr('id','p2'+cardid);//.append(row);
		if(count<=1){
			t.css('margin-top','0px')
		}else{
			t.css('margin-top','-100px')
		}
		t.attr('onClick','game.players[1].attack('+cardid+')');
		t.appendTo('#playerTwoCards');
		//$('#talon'+cardid).attr('id','#p2'+cardid).appendTo('#playerTwoCards').fadeIn(1000);
	}
	$('#talonCards').css('margin-top','100px');
}

// is this being used? ahgg...!
Table.prototype.reloadPlayer = function(playerId){
	if(playerId==1){
		var rows = [];
		var cards = game.talonCards.getCardsbyPlayerId(playerId);
		$(cards).each(function(key,card){
			if(!game.players[0].cards.isInPlayerTable(card.id)){
				var btn = $(newBtn);
				btn.attr('class','btn btn-danger');
				btn.attr('id','btn1');
				btn.attr('value','Use me');
				btn.attr('onClick','game.players[0].attack('+card.id+')');
//				if(this.whosTurn==2){
//					btn.css('display','none');
//				}
				var t = $('#talon'+card.id).attr('id','p1'+card.id).append($(newData).append(btn));//.append(row);
			}
		});
	}else if(playerId==2){
		
		var rows = [];
		var cards = game.talonCards.getCardsbyPlayerId(playerId);
		$(cards).each(function(key,card){
			var btn = $(newBtn);
			btn.attr('class','btn btn-danger');
			btn.attr('id','btn2');
			btn.attr('value','Use me')
			btn.attr('onClick','game.players[1].attack('+card.id+')');
//			if(this.whosTurn==1){
//				btn.css('display','none');
//			}
			var t = $('#talon'+card.id).attr('id','p2'+card.id).append($(newData).append(btn));//.append(row);
		});
	}
	return this;
}


Table.prototype.reLoadPlayersTable = function(playerId){
	//playerOneCards
	if(playerId==1){
		playerOneCards.html('');// clear data
//		var rowHead = $(newRow).attr('id','head');
//		rowHead.append($(newHead).text("id"));
//		rowHead.append($(newHead).text("Image"));
//		rowHead.append($(newHead).text("Acction"));
//		playerOneCards.append(rowHead);
		var cards = game.talonCards.getCardsbyPlayerId(playerId);
		console.log(cards.length);
		isFirst = true;
		$(cards).each(function(key,card){
//			var row = $(newRow).attr('id',"p1"+card.id); //talon id
//			row.append($(newData).text(card.id));
			var img = $(newImage).attr('src',resImagesPathCards+card.imageFilName);
			img.attr('height',150);
			img.attr('width',100);
			img.attr('onClick','game.players[0].attack('+card.id+')');
//			row.append($(newData).append(img));
//			var btn = $(newBtn);
//			btn.attr('class','btn btn-danger');
//			btn.attr('id',"btn1");
//			btn.attr('value','Use me');
//			btn.attr('onClick','game.players[0].attack('+card.id+')');
//			row.append($(newData).append(btn));
			var div = $(newDiv).append(img);
			div.attr('id',"p1"+card.id);
			if(isFirst){
				
				isFirst = false;
			}else{
				div.css('margin-top','-100px');
			}
			
			playerOneCards.append(div);
		});
	}else{
		playerTwoCards.html('');// clear data
//		var rowHead = $(newRow).attr('id','head');
//		rowHead.append($(newHead).text("id"));
//		rowHead.append($(newHead).text("Image"));
//		rowHead.append($(newHead).text("Acction"));
//		playerTwoCards.append(rowHead);
		var cards = game.talonCards.getCardsbyPlayerId(playerId);
		console.log(cards.length);
		isFirst = true;
		$(cards).each(function(key,card){
//			var row = $(newRow).attr('id',"p2"+card.id); //talon id
//			row.append($(newData).text(card.id));
			var img = $(newImage).attr('src',resImagesPathCards+card.imageFilName);
			img.attr('height',150);
			img.attr('width',100);
			img.attr('onClick','game.players[1].attack('+card.id+')');
//			row.append($(newData).append(img));
//			var btn = $(newBtn);
//			btn.attr('class','btn btn-danger');
//			btn.attr('id',"btn2");
//			btn.attr('value','Use me');
//			btn.attr('onClick','game.players[1].attack('+card.id+')');
//			row.append($(newData).append(btn));
			var div = $(newDiv).append(img);
			div.attr('id',"p2"+card.id);
			
			if(isFirst){
				isFirst = false;
			}else{
				div.css('margin-top','-100px');
			}
			playerTwoCards.append(div);
		});
	}
	return this;
}

Table.prototype.whosNextTurn = function(){
	//anonymous objects. created during runtime
	var btn1 = $('#btn1');
	var btn2 = $('#btn2');
	if(this.whosTurn==1){
		this.whosTurn=2;
		btn1.hide();
		btn2.show();
	}else{
		this.whosTurn=1;
		btn1.show();
		btn2.hide();
	}
}

Table.prototype.reLoadTurnTable = function(){
	console.log("reLoadTurnTable()");
	// TODO: NO BODY
}

Table.prototype.reLoadTalon = function(){
	console.log("reLoadTalon()");
	this.addTracerInfo(new Info({'description':'updating talons table','triggered':'reLoadTalon'}));
	talonCards = game.talonCards;
	cards = game.cards;
	talonCardsNotDrawn = [];
	console.log('total talonCards count: '+talonCards.length);
	//setting the image filename for each taloncards
	$(cards).each(function(key,card){
		$(talonCards).each(function(key,talonCard){
			if(card.id == talonCard.card_id){
				//console.log('card.id===talonCard.card_id = true');
				talonCards[key].imageFilName = card.image;
			}
			///console.log('talonCard.replenishee_id = '+talonCard.replenishee_id);
		});
	});
	$(talonCards).each(function(key,talonCard){
		if(talonCard.replenishee_id == '0' ){ //&& talonCard.active === '0'){ //& talonCard.active === 0){ // 0 means active sorrry for that.
			//console.log('talonCard.replenishee_id===0 ? = true');
			talonCardsNotDrawn.push(talonCard);
		}
	});
	
	talonTable.html('');// clear data
//	var rowHead = $(newRow).attr('id','head');
//	rowHead.append($(newHead).text("id"));
//	rowHead.append($(newHead).text("Image"));
//	talonTable.append(rowHead);
//	console.log('talonCardsNotDrawn : '+talonCardsNotDrawn.length);
	isFirst = true;
	$(talonCardsNotDrawn).each(function(key,card){
//		var row = $(newRow).attr('id','talon'+card.id); //talon id
//
//		row.append($(newData).text(card.id));
		var div = $(newDiv);
		div.attr('id','talon'+card.id);
		var img = $(newImage).attr('src',resImagesPathCards+card.imageFilName);
		img.attr('height',150);
		img.attr('width',100);
		if(isFirst){
			isFirst = false;
		}else{
			div.css('margin-top','-100px');
		}
		div.append($(newDiv).append(img));
		talonTable.append(div);
	});
	this.addTracerInfo(new Info({'description':'updating talons table[DONE]','triggered':'reLoadTalon'}));
	return this;
}

